
package com.joolun.cloud.upms.admin.controller;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import com.joolun.cloud.common.core.constant.CommonConstants;
import com.joolun.cloud.common.core.util.R;
import com.joolun.cloud.common.core.util.SensitiveUtils;
import com.joolun.cloud.common.log.annotation.SysLog;
import com.joolun.cloud.common.nacos.util.NacosConfigUtils;
import com.joolun.cloud.common.sms.config.SmsConfigProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.Yaml;
import java.util.HashMap;
import java.util.Map;

/**
 * 阿里短信配置
 *
 * @author www.joolun.com
 * @date 2021-7-24 14:44:15
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/smsconfig")
@Api(value = "smsConfig", tags = "阿里短信配置管理")
public class SmsConfigController {

	private NacosConfigUtils nacosConfigUtils;

    /**
     * 阿里短信配置查询
     * @param
     * @return R
     */
    @ApiOperation(value = "阿里短信配置查询")
    @GetMapping
    @PreAuthorize("@ato.hasAuthority('sys:smsconfig:get')")
    public R getBy() throws NacosException {
		ConfigService configService = nacosConfigUtils.getConfigService();
		String content = configService.getConfig(CommonConstants.CONFIG_DATA_ID_APPLICATION, CommonConstants.CONFIG_GROUP, CommonConstants.CONFIG_TIMEOUT_MS);
		Yaml yaml = new Yaml();
		Map<String,Object> confMap= yaml.load(content);
		Object object = confMap.get("sms");
		JSONObject jSONObject = JSONUtil.parseObj(object);
		SmsConfigProperties smsConfigProperties = jSONObject.toBean(SmsConfigProperties.class);
		//数据脱敏
		String sensitiveSecret = SensitiveUtils.process(smsConfigProperties.getAccessKeySecret());
		smsConfigProperties.setAccessKeySecret(sensitiveSecret);
		return R.ok(smsConfigProperties);
    }

    /**
     * 阿里短信配置修改
     * @param smsConfigProperties 阿里短信配置
     * @return R
     */
    @ApiOperation(value = "阿里短信配置修改")
    @SysLog("修改阿里短信配置")
    @PutMapping
    @PreAuthorize("@ato.hasAuthority('sys:smsconfig:edit')")
    public R updateById(@RequestBody SmsConfigProperties smsConfigProperties) throws NacosException {
		ConfigService configService = nacosConfigUtils.getConfigService();
		String content = configService.getConfig(CommonConstants.CONFIG_DATA_ID_APPLICATION, CommonConstants.CONFIG_GROUP, CommonConstants.CONFIG_TIMEOUT_MS);
		Yaml yaml = new Yaml();
		Map<String,Map> confMap= yaml.load(content);

		String accessKeySecret = confMap.get("sms").get("accessKeySecret")+"";
		String sensitiveAccessKeySecret = SensitiveUtils.process(accessKeySecret);
		if(!sensitiveAccessKeySecret.equals(smsConfigProperties.getAccessKeySecret())){
			accessKeySecret = smsConfigProperties.getAccessKeySecret();
		}

		Map smsMap = new HashMap();
		smsMap.put("regionId",smsConfigProperties.getRegionId());
		smsMap.put("accessKeyId",smsConfigProperties.getAccessKeyId());
		smsMap.put("accessKeySecret",accessKeySecret);
		confMap.put("sms",smsMap);
		String confStr = yaml.dumpAsMap(confMap);
		boolean rs = configService.publishConfig(CommonConstants.CONFIG_DATA_ID_APPLICATION, CommonConstants.CONFIG_GROUP,confStr,"yaml");
        return R.ok(rs);
    }

}
